package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Response;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.handler.GuliException;
import io.swagger.annotations.Api;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author zhaolie
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/eduservice/video")
@Api(description = "课程小节")
public class EduVideoController {

    @Resource
    private EduVideoService videoService;

    // 注入vodClient
    @Resource
    private VodClient vodClient;

    // 添加小节
    @PostMapping
    private Response addVideo(@RequestBody EduVideo eduVideo) {
        videoService.addVideo(eduVideo);
        return Response.SUCCESS();
    }

    // TODO 后面需要完善: 删除小节的时候,同时把视频删除
    @DeleteMapping("/{id}")
    private Response deleteVideo(@PathVariable String id) {
        // 根据小节id获取视频id
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            Response response = vodClient.removeVideo(videoSourceId);
            if (response.getCode() == 20001) {
                return Response.Failed();
            }
        }
        videoService.deleteVideo(id);
        return Response.SUCCESS();
    }

    @GetMapping("/{id}")
    private Response getVideo(@PathVariable String id) {
        EduVideo video = videoService.getVideo(id);
        return Response.SUCCESS().data("rows", video);
    }

    @PutMapping
    private Response updateVideo(@RequestBody EduVideo eduVideo) {
        videoService.updateVideo(eduVideo);
        return Response.SUCCESS();
    }

}

